package nsu.edu.zsq.service.impl.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import nsu.edu.zsq.bean.StuReward;
import nsu.edu.zsq.common.RequestHolder;
import nsu.edu.zsq.dao.StuRewardMapper;
import nsu.edu.zsq.service.sys.StudentRewardService;
import nsu.edu.zsq.service.sys.SysLogService;
import nsu.edu.zsq.util.BeanValidator;

/** 
* Description: 学生奖惩信息的service实现类<br>
*/
@Service
public class StudentRewardServiceImpl implements StudentRewardService {
    
    @Autowired
    private StuRewardMapper stuRewardMapper;
    @Autowired
    private SysLogService sysLogService;

    @Override
    public StuReward getStuReward(int id) {
        StuReward stuReward = stuRewardMapper.selectByPrimaryKey(id);
        return stuReward;
    }
    
    @Override
    public List<StuReward> listStuReward(String stuNo) {
        List<StuReward> listStuReward = stuRewardMapper.listStuRewardByStuNo(stuNo);
        return listStuReward;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveStuReward(StuReward stuReward) {
        BeanValidator.check(stuReward);
        stuReward.setOperator(RequestHolder.getCurrentSysUser().getAccount());
        stuReward.setOperatorTime(new Date());
        stuRewardMapper.insertSelective(stuReward);
        
        // 添加日志
        String instruction = "学生管理-奖惩信息管理-添加奖惩";
        sysLogService.saveSysLogBySysUser(null, stuReward, instruction);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteStuReward(int[] rewardIds) {
        List<StuReward> before = new ArrayList<StuReward>();
        for (int rewardId : rewardIds) {
            StuReward beforeStuReward = getStuReward(rewardId);
            before.add(beforeStuReward);
            stuRewardMapper.deleteByPrimaryKey(rewardId);
        }
        
        // 添加日志
        String instruction = null;
        if (rewardIds.length > 1) {
            instruction = "学生管理-奖惩信息管理-批量删除";
        } else if (rewardIds.length == 1) {
            instruction = "学生管理-奖惩信息管理-单个删除";
        }
        sysLogService.saveSysLogBySysUser(before, null, instruction);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStuReward(StuReward stuReward) {
        BeanValidator.check(stuReward);
        StuReward before = getStuReward(stuReward.getId());
        stuReward.setOperator(RequestHolder.getCurrentSysUser().getAccount());
        stuReward.setOperatorTime(new Date());
        stuRewardMapper.updateByPrimaryKeySelective(stuReward);
        
        // 添加日志
        String instruction = "学生管理-奖惩信息管理-编辑奖惩";
        sysLogService.saveSysLogBySysUser(before, stuReward, instruction);
    }

    
}
